package db.tables;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import control.ConnectionManage;


import db.parking.beans.Employee;


/**
 * AdminEmployee manages all requests From,To table Employee
 * @author Operations
 *
 */
public class AdminEmployee {
	
	private static Connection conn = ConnectionManage.getInstance().getConnection();

	/**
	 *  Get Authentication Details of Employee for comparison from DB table Employee 
	 * @param uName
	 * @param pass
	 * @return
	 * @throws SQLException
	 */
	public static Employee getRow(String uName,String pass) throws SQLException {

		String sql = "SELECT * FROM Employee WHERE username = ? AND password =?";
		ResultSet rs = null;

		try (
				PreparedStatement stmt = conn.prepareStatement(sql);
				){
			stmt.setString(1, uName);
			stmt.setString(2, pass);
			rs = stmt.executeQuery();

			if (rs.next()) {
				Employee bean = new Employee();
				bean.setEmpID(rs.getInt("empID"));
				bean.setEmpName(rs.getString("empName"));
				bean.setUsername(rs.getString("username"));
				bean.setPassword(rs.getString("password"));
				bean.setType(rs.getString("type"));
				bean.setLotNo(rs.getInt("lotNo"));
				
				return bean;
			} else {
				return null;
			}

		} catch (SQLException e) {
			System.err.println(e);
			return null;
		} finally {
			if (rs != null) {
				rs.close();
			}
		}

	}

	//return all employes order by employey type
	/**
	 * Returns an array of all employee's currently in DB table Employee
	 * @return
	 * @throws SQLException
	 */
	public static Employee[] getAllEmployees() throws SQLException {
		String sql = "SELECT * FROM employee ORDER BY type";
		ResultSet rs = null;
		try (PreparedStatement stmt = conn.prepareStatement(sql);) {
			rs = stmt.executeQuery();
			rs.last();
			int totalRows = rs.getRow();
			rs.beforeFirst();
			Employee bean[] = new Employee[totalRows];
			for (int i = 0; i < totalRows; i++)
				bean[i] = new Employee();
			int rowCount = 0;
			while (rs.next()) {
				bean[rowCount].setEmpID(rs.getInt("empID"));
				bean[rowCount].setEmpName(rs.getString("empName"));
				bean[rowCount].setUsername(rs.getString("username"));
				bean[rowCount].setPassword(rs.getString("password"));
				bean[rowCount].setType(rs.getString("type"));
				bean[rowCount].setLotNo(rs.getInt("lotNo"));
				rowCount++;
			}
			return bean;
		} catch (SQLException e) {
			System.err.println(e);
			return null;
		} finally {
			if (rs != null) {
				rs.close();
			}
		}
	}
}
